<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    colors:{
      type: Array,
      isNull:false,
      default:()=>{return ["#FF005A","#3888fa"]}
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null
    }
  },
  watch: {
    chartData:function (value,oldValue) {
      if(value != null && value != undefined) {
        this.initChart();
      }
    }
  },
  mounted() {
    this.initChart()
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }

    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }

    this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.__resizeHandler()
      }
    },
    setOptions({xData=[],legendData=[], seriesData=[],unit }) {
      let series = [];
      if(legendData != null && legendData.length>0) {
        legendData.forEach((m, i) => {
          series.push({
            name: m, itemStyle: {
              normal: {
                color: this.colors[i],
                lineStyle: {
                  color: this.colors[i],
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: seriesData[i],
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          })
        })
      }
      let option = {
        xAxis: {
          data: xData,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
            type: 'value',
          axisTick: {
            show: false
          },
          axisLabel: {
              formatter:'{value} '+unit
          },
        },
        legend: {
          show:legendData.length>1,
          data: legendData
        },
        series:series
      };
      this.chart.setOption(option);
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');
      this.setOptions(this.chartData)
    }
  }
}
</script>
